﻿using MESPro.DBManager;
using MESPro.Models.FunctionType;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RM.Controls
{
    public partial class ShowStoreProcedureLIst : Form
    {
        #region Public Constructors

        public ShowStoreProcedureLIst()
        {
            InitializeComponent();
            this.Load += ShowStoreProcedureLIst_Load;

        }

        #endregion Public Constructors

        #region 属性

        public List<string> StoreProcList { get; set; } = new List<string>();
        public string StoreProcedureName;
        /// <summary>
        /// 
        /// </summary>
        public StoreProInfo? StoreProInfo { get; set; } = new StoreProInfo();

        #endregion 属性

        #region Private 方法

        private async void ShowStoreProcedureLIst_Load(object sender, EventArgs e)
        {
            await Task.Run(() =>
             {
                 StoreProcList = DbContext.Db.DbMaintenance.GetProcList("ManagementServer");
             });
            this.listBox1.DataSource = null;
            this.listBox1.DataSource = StoreProcList;
        }
        private void ShowStoreProcedureLIst_FormClosed(object sender, FormClosedEventArgs e)
        {
            ;
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            this.listBox1.DataSource = null;
            await Task.Run(() =>
            {
                StoreProcList = DbContext.Db.DbMaintenance.GetProcList("ManagementServer");
            });

            this.listBox1.DataSource = StoreProcList;
        }

        #endregion Private 方法

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (sender is ListBox lbx && lbx.SelectedItem != null)
            {
                StoreProcedureName = lbx.SelectedItem.ToString();
                StoreProInfo = new StoreProInfo();
                StoreProInfo.StoreName = lbx.SelectedItem.ToString();
                var xxx = SqlHelperParameterCache.GetSpParameterSet(DbContext.conString, StoreProInfo.StoreName);
                this.listBox2.DataSource = null;
                this.listBox2.DataSource = xxx;
                var ps = xxx.ToList().Select(x => x.ParameterName);
                foreach (var item in ps)
                {
                    StoreProInfo.StoreParms += item+",";
                }


            }
        }

        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
